home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter7 / getsrch.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  266 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "getsrch.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "TreeView_GetISearchString()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. VOID AddItemsToTree( HWND hTree );
  107.  
  108. #define IMAGESIZE 16
  109.  
  110. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static char szBuf[40];
  115. static UINT uTimerID = 0;
  116. static HWND hTree    = NULL;
  117. static HWND hStatic  = NULL;
  118.  
  119.    switch( uMsg )
  120.    {
  121.       case WM_CREATE  :
  122.               {
  123.                  HIMAGELIST hImage;
  124.  
  125.                  InitCommonControls();
  126.  
  127.                  hImage = ImageList_Create( IMAGESIZE, IMAGESIZE, 
  128.                                             ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  129.                  
  130.                  hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
  131.                                          WS_CHILD | WS_BORDER | WS_VISIBLE | 
  132.                                          WS_VSCROLL | TVS_HASLINES | 
  133.                                          TVS_HASBUTTONS | TVS_LINESATROOT |
  134.                                          TVS_DISABLEDRAGDROP,
  135.                                          10, 100, 100, 100,
  136.                                          hWnd,
  137.                                          (HMENU)1,
  138.                                          hInst,
  139.                                          NULL);
  140.  
  141.                  hStatic = CreateWindowEx( WS_EX_CLIENTEDGE, "STATIC", "", 
  142.                                            WS_CHILD | WS_VISIBLE,
  143.                                            0, 0, 100, 20,
  144.                                            hWnd,
  145.                                            (HMENU)2,
  146.                                            hInst,
  147.                                            NULL );
  148.  
  149.                  if ( hTree )
  150.                  {
  151.                     int i;
  152.  
  153.                     // Associate the image list with the tree view.
  154.                     //.............................................
  155.                     TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
  156.  
  157.                     // Add the images to the tree view image list.
  158.                     //............................................
  159.                     for ( i=0; i<4; i++ )
  160.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  161.  
  162.                     AddItemsToTree( hTree );
  163.                  }
  164.  
  165.                  uTimerID = SetTimer( hWnd, 1, 500, NULL );
  166.               }
  167.               break;
  168.  
  169.  
  170.       case WM_SIZE :
  171.               if ( hStatic )
  172.                  MoveWindow( hStatic,0,0, LOWORD(lParam), 20, FALSE );
  173.  
  174.               if ( hTree )
  175.                  MoveWindow( hTree,0,20, LOWORD(lParam), HIWORD(lParam)-20, FALSE );
  176.               break;
  177.  
  178.  
  179.       case WM_TIMER :
  180.               if ( wParam == uTimerID )
  181.               {
  182.                  TreeView_GetISearchString( hTree, szBuf );
  183.                  SetWindowText( hStatic, szBuf );
  184.               }
  185.               break;
  186.  
  187.  
  188.       case WM_COMMAND :
  189.               switch( LOWORD( wParam ) )
  190.               {
  191.                  case IDM_ABOUT :
  192.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  193.                         break;
  194.  
  195.                  case IDM_EXIT :
  196.                         DestroyWindow( hWnd );
  197.                         break;
  198.               }
  199.               break;
  200.       
  201.       case WM_DESTROY :
  202.               KillTimer( hWnd, uTimerID );
  203.  
  204.               if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
  205.                  ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
  206.  
  207.               PostQuitMessage(0);
  208.               break;
  209.  
  210.       default :
  211.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  212.    }
  213.  
  214.    return( 0L );
  215. }
  216.  
  217.  
  218. VOID AddItemsToTree( HWND hTree )
  219. {
  220.    int             i;
  221.    TV_INSERTSTRUCT tv;
  222.  
  223.    tv.hInsertAfter = TVI_LAST;
  224.    tv.item.mask    = TVIF_TEXT | TVIF_IMAGE | 
  225.                      TVIF_SELECTEDIMAGE | TVIF_PARAM;
  226.  
  227.    // Add the items to the tree view.
  228.    //................................
  229.    for ( i=0; i<4; i++ )
  230.    {
  231.       tv.hParent      = TVI_ROOT;
  232.       tv.item.pszText = (LPTSTR)lpszData[i];
  233.       tv.item.iImage  = i;
  234.       tv.item.iSelectedImage = i;
  235.       tv.item.lParam         = i;
  236.  
  237.       TreeView_InsertItem( hTree, &tv );
  238.    }
  239. }
  240.  
  241.  
  242. LRESULT CALLBACK About( HWND hDlg,           
  243.                         UINT message,        
  244.                         WPARAM wParam,       
  245.                         LPARAM lParam)
  246. {
  247.    switch (message) 
  248.    {
  249.        case WM_INITDIALOG: 
  250.                return (TRUE);
  251.  
  252.        case WM_COMMAND:                              
  253.                if (   LOWORD(wParam) == IDOK         
  254.                    || LOWORD(wParam) == IDCANCEL)    
  255.                {
  256.                        EndDialog(hDlg, TRUE);        
  257.                        return (TRUE);
  258.                }
  259.                break;
  260.    }
  261.  
  262.    return (FALSE); 
  263. }
  264.  
  265.  
  266.